﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapICC_GLLGEX(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveICC_GLLGEX(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryICC_GLLGEX(dwData, 1);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapICC_GLLGEX(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertICC_GLLGEX(dwLocal);
            }
            UpdateICC_GLLGEX(dwLocal);
        }
        public void InsertICC_GLLGEX(DataRow dwData, int option = 1)
        {
            //GSCMANDANT  GSCART GSCVAR GSCFARBE
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1: 
                    or1.CommandText = "INSERT INTO ICC_GLLGEX "
                    + " SELECT DISTINCT ICCDAT, GPMANDANT,GLLO,GLLB,GPGART,GPGVAR,GPGFARBE, GPSPPART, GPZWPART, GPFBPART, GPBEM1,STRAGG(QTY || '*' || KG) OVER(PARTITION BY ICCDAT, GPMANDANT,GLLO,GLLB, GPSPPART, GPZWPART, GPFBPART, GPBEM1) AS PATTEN  FROM (SELECT DISTINCT ICCDAT, GPMANDANT,GLLO,GLLB, GPGART,GPGVAR,GPGFARBE,GPSPPART, GPZWPART, GPFBPART, GPBEM1,GLBSTKG * 0.001 AS KG,COUNT(1) OVER(PARTITION BY GPMANDANT,GLLO,GLLB, GPGART,GPGVAR,GPGFARBE,GPSPPART, GPZWPART, GPFBPART, GPBEM1, GLBSTKG) AS QTY          FROM ICC_GLLG JOIN GPDAT ON GPPARTIE=GLPARTIE         WHERE ICCDAT =:ICCDAT)";
                    or1.Parameters.Add("ICCDAT", OracleType.NVarChar).Value = dwData["MDATE"].ToString();
                    break;
                case 2:
                    or1.CommandText = "INSERT INTO ICC_GLLGEX "
                    + " SELECT DISTINCT ICCDAT, GPMANDANT,GLLO,GLLB,GPGART,GPGVAR,GPGFARBE, GPSPPART, GPZWPART, GPFBPART, GPBEM1,STRAGG(QTY || '*' || KG) OVER(PARTITION BY ICCDAT, GPMANDANT,GLLO,GLLB, GPSPPART, GPZWPART, GPFBPART, GPBEM1) AS PATTEN  FROM (SELECT DISTINCT :ICCDAT AS ICCDAT, GPMANDANT,GLLO,GLLB, GPGART,GPGVAR,GPGFARBE,GPSPPART, GPZWPART, GPFBPART, GPBEM1,  GLBSTKG * 0.001 AS KG,   COUNT(1) OVER(PARTITION BY GPMANDANT,GLLO,GLLB, GPGART,GPGVAR,GPGFARBE,GPSPPART, GPZWPART, GPFBPART, GPBEM1, GLBSTKG) AS QTY   FROM GLLG JOIN GPDAT ON GPPARTIE=GLPARTIE)";
                    or1.Parameters.Add("ICCDAT", OracleType.NVarChar).Value = dwData["MDATE"].ToString();
                    break;
            }
            setResult = Connect2Proxy(or1);
        }
        public void DeleteICC_GLLGEX(DataRow dwData, int option = 1)
        {
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM  ICC_GLLGEX WHERE "
    + "  ICCDAT=:ICCDAT ";
                    or1.Parameters.Add("ICCDAT", OracleType.NVarChar).Value = dwData["MDATE"].ToString();
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryICC_GLLGEX(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "ICC_GLLGEX";
            }

            setReturn.Merge(Result);
            return setReturn;
        }
        public void UpdateICC_GLLGEX(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    break;
            }

            Connect2Proxy(or1);
        }
    }
}
